package net.engio.mbassy.camel;

import net.engio.mbassy.MBassador;

import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Mbassador producer.
 */
public class MbassadorProducer extends DefaultProducer {

    private static final transient Logger LOG = LoggerFactory.getLogger(MbassadorProducer.class);

    /**
     * MBassador event bus object instance.
     */
    private final MBassador eventBus;

    /**
     * Creates a new instance of the MbassadorProducer class.
     * @param endpoint {@link MbassadorEndpoint} object instance
     * @param eventBus {@link MBassador} event bus object instance
     */
    public MbassadorProducer(final MbassadorEndpoint endpoint, final MBassador<?> eventBus) {
        super(endpoint);
        this.eventBus = eventBus;
    }

    @Override
    public void process(final Exchange exchange) throws Exception {
        final Object body = exchange.getIn().getBody();
        if (null == body) {
            LOG.debug("Body is null, cannot post to MBassador");
            return;
        }
        LOG.debug("Posting: {} to MBassador: {}", body, eventBus);
        eventBus.publishAsync(body);
    }

}
